Completed
Push — master ( 2f8415...e6e0b0 )
by Rain
02:22
created

Security.js ➔ ???   B

Complexity

Conditions 1
Paths 2

Size

Total Lines 90

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 1
c 6
b 0
f 0
nc 2
nop 0
dl 0
loc 90
rs 8.5454

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
2
import _ from '_';
3
import ko from 'ko';
4
5
import {trim, boolToAjax} from 'Common/Utils';
6
import {phpInfo} from 'Common/Links';
7
import {StorageResultType, Magics} from 'Common/Enums';
8
9
import {settingsGet} from 'Storage/Settings';
10
11
import AppAdminStore from 'Stores/Admin/App';
12
import CapaAdminStore from 'Stores/Admin/Capa';
13
14
import Remote from 'Remote/Admin/Ajax';
15
16
import {command} from 'Knoin/Knoin';
17
18
class SecurityAdminSettings
19
{
20
	constructor() {
21
		this.useLocalProxyForExternalImages = AppAdminStore.useLocalProxyForExternalImages;
22
23
		this.weakPassword = AppAdminStore.weakPassword;
24
25
		this.capaOpenPGP = CapaAdminStore.openPGP;
26
27
		this.capaTwoFactorAuth = CapaAdminStore.twoFactorAuth;
28
		this.capaTwoFactorAuthForce = CapaAdminStore.twoFactorAuthForce;
29
30
		this.capaTwoFactorAuth.subscribe((value) => {
31
			if (!value)
32
			{
33
				this.capaTwoFactorAuthForce(false);
34
			}
35
		});
36
37
		this.verifySslCertificate = ko.observable(!!settingsGet('VerifySslCertificate'));
38
		this.allowSelfSigned = ko.observable(!!settingsGet('AllowSelfSigned'));
39
40
		this.verifySslCertificate.subscribe((value) => {
41
			if (!value)
42
			{
43
				this.allowSelfSigned(true);
44
			}
45
		});
46
47
		this.isTwoFactorDropperShown = ko.observable(false);
48
		this.twoFactorDropperUser = ko.observable('');
49
		this.twoFactorDropperUser.focused = ko.observable(false);
50
51
		this.adminLogin = ko.observable(settingsGet('AdminLogin'));
52
		this.adminLoginError = ko.observable(false);
53
		this.adminPassword = ko.observable('');
54
		this.adminPasswordNew = ko.observable('');
55
		this.adminPasswordNew2 = ko.observable('');
56
		this.adminPasswordNewError = ko.observable(false);
57
58
		this.adminPasswordUpdateError = ko.observable(false);
59
		this.adminPasswordUpdateSuccess = ko.observable(false);
60
61
		this.adminPassword.subscribe(() => {
62
			this.adminPasswordUpdateError(false);
63
			this.adminPasswordUpdateSuccess(false);
64
		});
65
66
		this.adminLogin.subscribe(() => {
67
			this.adminLoginError(false);
68
		});
69
70
		this.adminPasswordNew.subscribe(() => {
71
			this.adminPasswordUpdateError(false);
72
			this.adminPasswordUpdateSuccess(false);
73
			this.adminPasswordNewError(false);
74
		});
75
76
		this.adminPasswordNew2.subscribe(() => {
77
			this.adminPasswordUpdateError(false);
78
			this.adminPasswordUpdateSuccess(false);
79
			this.adminPasswordNewError(false);
80
		});
81
82
		this.onNewAdminPasswordResponse = _.bind(this.onNewAdminPasswordResponse, this);
83
	}
84
85
	@command((self) => '' !== trim(self.adminLogin()) && '' !== self.adminPassword())
86
	saveNewAdminPasswordCommand() {
87
88
		if ('' === trim(this.adminLogin()))
89
		{
90
			this.adminLoginError(true);
91
			return false;
92
		}
93
94
		if (this.adminPasswordNew() !== this.adminPasswordNew2())
95
		{
96
			this.adminPasswordNewError(true);
97
			return false;
98
		}
99
100
		this.adminPasswordUpdateError(false);
101
		this.adminPasswordUpdateSuccess(false);
102
103
		Remote.saveNewAdminPassword(this.onNewAdminPasswordResponse, {
104
			'Login': this.adminLogin(),
105
			'Password': this.adminPassword(),
106
			'NewPassword': this.adminPasswordNew()
107
		});
108
109
		return true;
110
	}
111
112
	showTwoFactorDropper() {
113
		this.twoFactorDropperUser('');
114
		this.isTwoFactorDropperShown(true);
115
116
		_.delay(() => {
117
			this.twoFactorDropperUser.focused(true);
118
		}, Magics.Time50ms);
119
	}
120
121
	onNewAdminPasswordResponse(result, data) {
122
		if (StorageResultType.Success === result && data && data.Result)
123
		{
124
			this.adminPassword('');
125
			this.adminPasswordNew('');
126
			this.adminPasswordNew2('');
127
128
			this.adminPasswordUpdateSuccess(true);
129
130
			this.weakPassword(!!data.Result.Weak);
131
		}
132
		else
133
		{
134
			this.adminPasswordUpdateError(true);
135
		}
136
	}
137
138
	onBuild() {
139
		this.capaOpenPGP.subscribe((value) => {
140
			Remote.saveAdminConfig(null, {
141
				'CapaOpenPGP': boolToAjax(value)
142
			});
143
		});
144
145
		this.capaTwoFactorAuth.subscribe((value) => {
146
			Remote.saveAdminConfig(null, {
147
				'CapaTwoFactorAuth': boolToAjax(value)
148
			});
149
		});
150
151
		this.capaTwoFactorAuthForce.subscribe((value) => {
152
			Remote.saveAdminConfig(null, {
153
				'CapaTwoFactorAuthForce': boolToAjax(value)
154
			});
155
		});
156
157
		this.useLocalProxyForExternalImages.subscribe((value) => {
158
			Remote.saveAdminConfig(null, {
159
				'UseLocalProxyForExternalImages': boolToAjax(value)
160
			});
161
		});
162
163
		this.verifySslCertificate.subscribe((value) => {
164
			Remote.saveAdminConfig(null, {
165
				'VerifySslCertificate': boolToAjax(value)
166
			});
167
		});
168
169
		this.allowSelfSigned.subscribe((value) => {
170
			Remote.saveAdminConfig(null, {
171
				'AllowSelfSigned': boolToAjax(value)
172
			});
173
		});
174
	}
175
176
	onHide() {
177
		this.adminPassword('');
178
		this.adminPasswordNew('');
179
		this.adminPasswordNew2('');
180
181
		this.isTwoFactorDropperShown(false);
182
		this.twoFactorDropperUser('');
183
		this.twoFactorDropperUser.focused(false);
184
	}
185
186
	/**
187
	 * @returns {string}
188
	 */
189
	phpInfoLink() {
190
		return phpInfo();
191
	}
192
}
193
194
export {SecurityAdminSettings, SecurityAdminSettings as default};
195